package com.example1.polaris;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;

import android.annotation.SuppressLint;
import android.app.Activity;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;

import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Usuario extends Activity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.usu);
	
		
		Button BTIu=(Button)findViewById(R.id.INSu);
		Button BTEu=(Button)findViewById(R.id.ELIu);
		
				
        final EditText Id_Usuario=(EditText)findViewById(R.id.ID);
        final EditText User_Name=(EditText)findViewById(R.id.NOMBRE);
        final EditText Password=(EditText)findViewById(R.id.PASSWORD);
       
        
        BTIu.setOnClickListener(new View.OnClickListener()
        
        {
			
			@SuppressLint("ShowToast") @Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try{
					httpGetData("http://10.0.2.2/sistemapolaris/registrarUsuario.php?Id_Usuario="+Id_Usuario.getText()+
							"&User_Name="+User_Name.getText()+"&Password="+Password.getText());
					
					Toast.makeText(getApplicationContext(), "El dato ha sido actualizado correctamente", 1000).show();
				}catch(Exception e){
					Toast.makeText(getApplicationContext(), "Error en el envio de la informacion, verifique su conexion a internet y vuelva a intentarlo.", 1000).show();
					
				}
				Id_Usuario.setText("");
				User_Name.setText("");
				Password.setText("");
				
				
			}
		});
        
        BTEu.setOnClickListener(new View.OnClickListener()
        
        	{
			
			@SuppressLint("ShowToast") @Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try{
					httpGetData("http://10.0.2.2/sistemapolaris/eliminarUsuario.php?Id_Usuario="+Id_Usuario.getText()+
							"&User_Name="+User_Name.getText()+"&Password="+Password.getText());
					
					Toast.makeText(getApplicationContext(), "El dato ha sido eliminado", 1000).show();
				}catch(Exception e){
					Toast.makeText(getApplicationContext(), "Error en el envio de la informacion, verifique su conexion a internet y vuelva a intentarlo.", 1000).show();
					
				}
				Id_Usuario.setText("");
				User_Name.setText("");
				Password.setText("");
				
			}
		});
        
        
        User_Name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				// TODO Auto-generated method stub
				if(hasFocus==false){
					JSONArray ja=null;
					try {
						String data;
						data=httpGetData("http://10.0.2.2/sistemapolaris/consultarUsuario.php?User_Name="+User_Name.getText());
						if(data.length()>1)
							ja=new JSONArray(data);
						
						
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						Toast.makeText(getApplicationContext(), "Error recuperando la informacion del servidor, verifique su conexion a internet y vuelva a intentarlo.", 1000).show();
						
					}
					try{
						
					Id_Usuario.setText(ja.getString(0));
					Password.setText(ja.getString(2));

					} catch (Exception e) {

						Id_Usuario.setText("");
						Password.setText("");
					}
				}
			}
		});

        
    }
    

    public String httpGetData(String mURL) {
           String response="";
           mURL=mURL.replace(" ", "%20");
            Log.i("LocAndroid Response HTTP Threas","Ejecutando get 0: "+mURL);
             HttpClient httpclient = new DefaultHttpClient();
             
            Log.i("LocAndroid Response HTTP Thread","Ejecutando get 1");
       	HttpGet httppost = new HttpGet(mURL);
            Log.i("LocAndroid Response HTTP Thread","Ejecutando get 2");
   try {
      

            Log.i("LocAndroid Response HTTP","Ejecutando get");
           // Execute HTTP Post Request
         ResponseHandler<String> responseHandler=new BasicResponseHandler();
           	response = httpclient.execute(httppost,responseHandler);
            Log.i("LocAndroid Response HTTP",response);
       	} catch (ClientProtocolException e) {
           Log.i("LocAndroid Response HTTP ERROR 1",e.getMessage());
           // TODO Auto-generated catch block
       } catch (IOException e) {
           
           Log.i("LocAndroid Response HTTP ERROR 2",e.getMessage());
           // TODO Auto-generated catch block
       }
       return response;
       
       }  


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

